package utils

import (
	"fmt"
	"gitee.com/chejiangyi/bsfgo/core/base2"
	"os"
	"path/filepath"
)

var FileUtil = &FileUtilTool{}

type FileUtilTool struct{}

func (*FileUtilTool) FileExist(filename string) bool {
	_, err := os.Stat(filename)
	if err == nil {
		return true
	}
	return false
}

func (*FileUtilTool) FileAbs(filename string) string {
	filename2, err := filepath.Abs(filename)
	if err == nil {
		return filename2
	}
	panic(base2.NewBsfError(fmt.Sprintf("文件获取绝对路径出错: %s", filename)))
}

func (*FileUtilTool) FilterNoExistingFiles(filenames []string) []string {
	var existingFiles []string
	for _, filename := range filenames {
		if _, err := os.Stat(filename); err == nil {
			existingFiles = append(existingFiles, filename)
		}
	}
	return existingFiles
}
